package cn.le.messagePack;

import cn.le.bean.vo.NettyMsg;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToMessageDecoder;
import org.msgpack.MessagePack;

import java.util.List;

public class MessagePackDecoder extends MessageToMessageDecoder<ByteBuf> {

    @Override
    protected void decode(ChannelHandlerContext channelHandlerContext, ByteBuf msg, List<Object> list) throws Exception {
        int len = msg.readableBytes();//获取可读数据长度
        byte[] data = new byte[len];
        msg.getBytes(msg.readerIndex(),data,0,len);
        MessagePack pack = new MessagePack();
        NettyMsg nettyMsg = pack.read(data,pack.lookup(NettyMsg.class));
        list.add(nettyMsg);
    }
}
